home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9989 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.1 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: ?? How to dump text files to screen ??
  5. Date: Thu, 14 Mar 96 20:56:51 GMT
  6. Organization: none
  7. Message-ID: <826837011snz@genesis.demon.co.uk>
  8. References: <31430CE8.469E@aol2.com> <4i2pv1$it4@library.erc.clarkson.edu>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4i2pv1$it4@library.erc.clarkson.edu>
  15.            koglerje@craft.camp.clarkson.edu "Mountain (jimbo" writes:
  16.  
  17. >Neil (neil@aol2.com) wrote:
  18. >: I need help -- how can I open up a text file and just dump it
  19. >: all out (in plain text) to the screen?
  20. >
  21. >: Please post solution or mailto:neil@aol2.com
  22. >
  23. >: Thanks!
  24. >: Neil Mansilla
  25. >
  26. >Open a the file, then read in charters until the end, echo the charaters
  27. >to the screen. done like so:
  28. >----
  29. >include <stdio.h>
  30. >main()
  31.  
  32. This is OK but is an obsolete form of:
  33.  
  34. int main(void)
  35.  
  36. >{
  37. > FILE *fptr;
  38. > char ch;
  39.  
  40. It isn't guaranteed that EOF can be stored in a char. Make ch an int
  41. instead.
  42.  
  43. > fptr = fopen("YOUR FILE NAME AND PATH","+r");
  44. > if (fptr == NULL)
  45. >   {printf("\nERROR, NO FILE FOUND\n");
  46. >   return(-1);
  47.  
  48. What does -1 mean here? The only values that have any meaning defined
  49. by the C language are EXIT_SUCCESS, EXIT_FAILURE and 0 (which is also a
  50. value indicating success). EXIT_SUCCESS and EXIT_FAILURE are defined in
  51. stdlib.h.
  52.  
  53. >   }
  54. > while (ch != EOF)
  55.  
  56. The C language is not predeterministic - you must assign a value to
  57. a variable before you can test that value.
  58.  
  59. >   { ch=fgetch(fptr);
  60. >     fputch(stderr,ch);
  61.  
  62. Why stderr rather than stdout? If ytou have a good reason you should state
  63. it.
  64.  
  65. fgetch() and fputch() are not part of the standard C library and can't be
  66. defined in stdio.h on a conforming implementation.
  67.  
  68. >   }
  69. >}
  70.  
  71. Instead try:
  72.  
  73.   while ((ch = getc(fptr) != EOF)
  74.      putc(ch, stderr);             /* or stdout */
  75.  
  76.  
  77. -- 
  78. -----------------------------------------
  79. Lawrence Kirby | fred@genesis.demon.co.uk
  80. Wilts, England | 70734.126@compuserve.com
  81. -----------------------------------------
  82.